﻿using HCQ2_Common;
using HCQ2_Common.Data;
using HCQ2_Common.SQL;
using HCQ2_IDAL;
using HCQ2_Model;
using HCQ2_Model.VR.Model;
using HCQ2_Model.VR.Params;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace HCQ2_DAL_MSSQL
{
    public partial class T_VRExperienceMainDAL: IT_VRExperienceMainDAL
    {
        /// <summary>
        ///  获取课件所在目录
        /// </summary>
        /// <param name="file_id"></param>
        /// <returns></returns>
        public string GetFolderName(int file_id)
        {
            sb?.Clear();
            sb.Append($@"SELECT folder.folder_name FROM 
            (SELECT folder_id FROM dbo.T_DocumentFolderRelation WHERE file_id={file_id})rel INNER JOIN
            (SELECT folder_name,folder_id FROM dbo.T_DocumentFolder)folder ON rel.folder_id = folder.folder_id;");
            return Helper.ToString(SqlHelper.ExecuteScalar(sb.ToString()));
        }
        /// <summary>
        ///  获取个人学校、班级信息
        /// </summary>
        /// <param name="user_id"></param>
        /// <returns></returns>
        public ExperienceMainResult GetSchoolInfo(int user_id)
        {
            sb?.Clear();
            sb.Append($@"SELECT class.schoolName,class.schoolCode,class.className FROM 
            (SELECT schoolName,schoolCode,className,id FROM dbo.T_Class)class INNER JOIN
            (SELECT studentName,classId,studentId FROM dbo.T_Student)student ON class.id=student.classId INNER JOIN
            (SELECT user_id,studentId FROM dbo.T_User WHERE user_id={user_id})users ON users.studentId = student.studentId;");
            return SqlHelper.ExecuteList<ExperienceMainResult>(sb.ToString())?.FirstOrDefault();
        }
        /// <summary>
        ///  获取教学统计列表
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        public List<T_VRExperienceMain> GetExperienceList(ExperienceParam param, out int total)
        {
            sb?.Clear();
            string temp = "SELECT * FROM dbo.T_VRExperienceMain WHERE 1=1 ";
            if (!string.IsNullOrEmpty(param.keyword))
                sb.Append($" AND user_name LIKE '%{param.keyword}%' ");
            switch (param.proStatus)
            {
                case "ok":sb.Append(" AND learn_len>=finsh_len ");break;
                case "err":sb.Append(" AND learn_len<finsh_len ");break;
            }
            if (!string.IsNullOrEmpty(param.code))
            {
                if (param.is_unit)
                    sb.Append($" AND schoolCode='{param.code}' ");
                else
                    sb.Append($" AND classId={param.code} ");
            }
            total = Helper.ToInt(SqlHelper.ExecuteScalar("SELECT COUNT(0) FROM dbo.T_VRExperienceMain WHERE 1=1 " + sb.ToString()));
            return SqlHelper.ExecutePageList<T_VRExperienceMain>(param.page, param.rows, "em_id", temp + sb.ToString());
        }
    }
}